{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 作业2\n",
    "（求出给定的excel文件的温度平均值，保存在第30行并显示出来。）\n",
    "### 通义千问：\n",
    "将C:\\python作业\\data中2023-09-13.xlsx温度的平均值计算出来，保存在A列30行并显示出来"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "平均温度: 49.07277585787494\n",
      "平均温度已成功写入文件的A列第30行。\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from openpyxl import load_workbook\n",
    "\n",
    "# 定义文件路径\n",
    "file_path = r'C:\\python作业\\data\\2023-09-13.xlsx'\n",
    "\n",
    "# 读取Excel文件，默认读取第一个工作表\n",
    "df = pd.read_excel(file_path)\n",
    "\n",
    "# 假设温度数据在名为'Temperature'的列中，请根据实际情况调整列名\n",
    "temperature_column = '温度'  # 替换为实际的温度列名\n",
    "\n",
    "if temperature_column in df.columns:\n",
    "    # 计算平均温度\n",
    "    avg_temperature = df[temperature_column].mean()\n",
    "    \n",
    "    # 打印平均温度\n",
    "    print(f\"平均温度: {avg_temperature}\")\n",
    "\n",
    "    # 使用openpyxl加载工作簿和选定的工作表\n",
    "    wb = load_workbook(filename=file_path)\n",
    "    ws = wb.active  # 或者通过名称选择工作表：ws = wb['SheetName']\n",
    "\n",
    "    # 写入平均温度到A30\n",
    "    ws['A30'] = avg_temperature\n",
    "\n",
    "    # 保存工作簿\n",
    "    wb.save(file_path)\n",
    "\n",
    "    print(\"平均温度已成功写入文件的A列第30行。\")\n",
    "else:\n",
    "    print(f\"未找到名为'{temperature_column}'的温度数据列，请检查您的文件和列名。\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
